package com.dhcc.core.modules.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.constant.state.PermPrincipalType;
import com.dhcc.core.framework.constant.state.ResourceGroup;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.dao.PermissionMapper;
import com.dhcc.core.modules.system.dao.ResourceMapper;
import com.dhcc.core.modules.system.dao.RoleMapper;
import com.dhcc.core.modules.system.entity.Permission;
import com.dhcc.core.modules.system.service.IPermissionService;

/**
 * 权限服务
 * 
 * @ClassName: PermissionServiceImpl
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:40:57
 */
@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    ResourceMapper resourceMapper;
    
    @Override
    public Map<String, List<Long>> loadPermsByResourceId(Long resourceId) {
        Wrapper<Permission> rrw = new EntityWrapper<>();
        rrw.eq("resource_id", resourceId);
        Map<String, List<Long>> resMap = new HashMap<>();
        List<Long> roleIds = new ArrayList<>();
        List<Long> deptIds = new ArrayList<>();
        List<Long> userIds = new ArrayList<>();
        List<Permission> roles = this.permissionMapper.selectList(rrw);
        if(CommonUtil.isNotEmpty(roles)){
            for (Permission rr : roles) {
                if(rr.getPrincipalType()==PermPrincipalType.ROLE.getCode()){
                    roleIds.add(rr.getPrincipalId());
                }
                if(rr.getPrincipalType()==PermPrincipalType.DEPT.getCode()){
                    deptIds.add(rr.getPrincipalId());
                }
                if(rr.getPrincipalType()==PermPrincipalType.USER.getCode()){
                    userIds.add(rr.getPrincipalId());
                }
            }
        }
        resMap.put("role", roleIds);
        resMap.put("dept", deptIds);
        resMap.put("user", userIds);
        return resMap;
    }

    @Override
    public void authPerm(Long resourceId, Long principalId, int principalType, Boolean isAdd){
        Permission entity = new Permission();
        entity.setResourceId(resourceId);
        entity.setPrincipalId(principalId);
        entity.setPrincipalType(principalType);
        Permission rr = this.permissionMapper.selectOne(entity);
        if(isAdd){
            if(CommonUtil.isNotEmpty(rr)){
                return;
            }
            entity.insert();
        }else{
            if(CommonUtil.isNotEmpty(rr)){
                rr.deleteById();
            }
        }
        return;
    }

    @Override
    public Map<String, List<Long>> loadPermsByPrincipalId(Long principalId, Integer principalType) {
        Wrapper<Permission> rrw = new EntityWrapper<>();
        rrw.eq("principal_id", principalId);
        rrw.eq("principal_type", principalType);
        Map<String, List<Long>> resMap = new HashMap<>();
        List<Long> backendIds = new ArrayList<>();
        List<Long> frontIds = new ArrayList<>();
        List<Long> apiIds = new ArrayList<>();
        List<Map<String, Object>> ls = this.permissionMapper.selectResIdByPrincipal(principalId,principalType);
        if(CommonUtil.isNotEmpty(ls)){
            for (Map<String, Object> rr : ls) {
                Long resourceId = Long.parseLong(rr.get("resourceId")+"");
                Integer resGroup = Integer.parseInt(rr.get("resGroup")+"");
                if(resGroup == ResourceGroup.BACKEND.getCode()){
                    backendIds.add(resourceId);
                }
                if(resGroup == ResourceGroup.FRONT.getCode()){
                    frontIds.add(resourceId);
                }
                if(resGroup == ResourceGroup.API.getCode()){
                    apiIds.add(resourceId);
                }
            }
        }
        resMap.put("backend", backendIds);
        resMap.put("front", frontIds);
        resMap.put("api", apiIds);
        return resMap;
    }
}
